// pages/orderDetail/orderDetail.js
const config = require("../../config/config");
const {
  OrderService
} = require("../../services/order_service");
import Toast from "../../miniprogram_npm/@vant/weapp/toast/toast";
import uploadFile from "../../utils/uploadImage";
var plugin = requirePlugin("logisticsPlugin")
var id;

Page({
  data: {
    order: {},
    appId: config.appId,
  },
  onLoad: function (options) {
    id = options.id;
    this.getDetail(options.id);
  },

  async getDetail(id) {
    let orderService = new OrderService();
    var res = await orderService.getOrderDetail(id);
    this.setData({
      order: res.data
    });
  },

  goServicePage() {
    wx.openCustomerServiceChat({
      extInfo: {
        url: config.customerServiceUrl
      },
      corpId: config.corpId,
      success(res) {}
    });
  },

  saveImage() {
    if (this.data.order.photo?.image) {
      wx.getImageInfo({
        src: this.data.order.photo.image,
        success: (res) => {
          wx.saveImageToPhotosAlbum({
            filePath: res.path,
            success: (res) => {
              wx.showToast({
                title: '保存成功',
                icon: 'success'
              })
            },
            fail:(res)=>{
              wx.showToast({
                title: res,
                icon: 'success'
              })
            },
            complete:(res)=>{
              setTimeout(() => {
                wx.previewImage({
                  urls: [this.data.order.photo.image],
                })
              }, 1000);
            }
          })
        }
      })
    } else {
      wx.showToast({
        title: '图片生成中，请下拉刷新',
        icon: 'none'
      })
    }
  },

  savePrintImage() {
    if (this.data.order.printImage) {
      wx.getImageInfo({
        src: this.data.order.printImage,
        success: (res) => {
          wx.saveImageToPhotosAlbum({
            filePath: res.path,
            success: (res) => {
              wx.showToast({
                title: '保存成功',
                icon: 'success'
              })
            }
          })
        }
      })
    } else {
      wx.showToast({
        title: '图片生成中，请下拉刷新',
        icon: 'none'
      })
    }
  },

  saveReceipt() {
    wx.getImageInfo({
      src: this.data.order.receiptImage,
      success: (res) => {
        wx.saveImageToPhotosAlbum({
          filePath: res.path,
          success: (res) => {
            wx.showToast({
              title: '保存成功',
              icon: 'success'
            })
          }
        })
      }
    })
  },

  getWaybillInfo() {
    plugin.openWaybillTracking({
      waybillToken: this.data.order.waybillToken
    })
  },

  onPullDownRefresh() {
    this.getDetail(id).then(res => {
      console.log(res);
      wx.stopPullDownRefresh();
    });
  },

  copyOrderNum() {
    wx.setClipboardData({
      data: this.data.order.orderNum,
    })
  },

  copyimage() {
    // wx.showToast({
    //   title: ''+this.data.order.photo.image,
    //   icon:'none'
    // })
    console.log(this.data.order.photo.image)
    wx.setClipboardData({
      data: this.data.order.photo.image,
    })
  },

  reupload() {
    wx.requestSubscribeMessage({
      tmplIds: [config.templateMessageId],
      complete: () => {
        wx.showActionSheet({
          itemList: [
            '相册选取',
            '现在拍摄'
          ],
          success: res => {
            if (res.tapIndex === 0) {
              this.selectPhoto()
            } else {
              wx.navigateTo({
                url: `/pages/camera/camera?productId=${this.data.order.product.id}&releatedId=${this.data.order.product.releatedId}&category=${this.data.order.product.category}&orderId=${this.data.order.id}`,
              })
            }
          }
        })
      }
    })
  },

  selectPhoto() {
    wx.chooseMedia({
      sourceType: ['album'],
      mediaType: ['image'],
      sizeType: ['original'],
      count: 1,
      success: async (res) => {
        Toast.loading({
          message: '上传中',
          forbidClick: true
        })
        let tempFilePath = res.tempFiles[0].tempFilePath;
        const originalImageUrl = await uploadFile(tempFilePath).catch(err => {
          console.log(err);
          wx.showToast({
            title: '上传图片失败',
            icon: 'error'
          })
          return;
        });
        if (originalImageUrl) {
          wx.navigateTo({
            url: `/pages/photoDetect/photoDetect?originalImagePath=${tempFilePath}&releatedId=${this.data.order.product.releatedId}&productId=${this.data.order.product.id}&originalImageUrl=${originalImageUrl}&category=${this.data.order.product.category}&orderId=${this.data.order.id}`
          })
        }
      }
    })
  },
})